<template>
  <div style="width: inherit;height: inherit;">
    <el-tooltip effect="light" :content="inputval" placement="top" :disabled="!inputval">
		<el-input v-model="inputval"
              :readonly="readonly"
              :disabled="disabled"
              :clearable="clearable"
              @focus="focus"
              @blur="blur"
              @change="change"
              @input="input"></el-input>
	</el-tooltip>
			  
  </div>
</template>

<script>
import { dateFormat, digitUppercase, round } from '@/utils/public.js'
export default {
  name: "dmInput",
  components: {
  },
  model: {
    prop: 'val',
    event: 'change'
  },
  props: {
    val: { default: null },//值
    formatText: { default: null },
    readonly: { default: false },
    disabled: { default: false },
    clearable: { default: false },
    testInfo: { default: null }
  },
  watch: {
    // 反向选择：监听value，当value因外界修改发生改变时选中自动选中option
    val: function (newValue) {
      //   console.log(newValue,this.testInfo)
      this.formatV();
    }
  },
  data () {
    return {
      realvalue: null,
      formatValue: null,
      inputval: null,
    };
  },
  mounted () {
    this.formatV();
    // console.log(this.val,this.testInfo)
    },
    methods: {
    	formatV(){
    		//格式化时间: time:y-m-d
    		//小数点: round: 2       //num.toFixed(2)
    		//财务每3位逗号隔开：  split:3
    		//财务金额转大写：upper:
    		//前缀：prefix：自己写
    		//后缀：suffix：自己写
    		//多个条件用逗号隔开
//var b = a.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');   //  (\d)(?=(\d{3})+\.)  带小数点的 可以转换成逗号隔开
// text.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') 不带小数点的逗号隔开 /(d)(?=(d{3})+.)/g

    		this.realvalue = this.val;
    		if(this.formatText){

    			let arr = this.formatText.split(',');

    			let temp = [];
    			for(let i=0;i<6;i++){//排序
    				for(let j=0;j<arr.length;j++){
    					let treg = arr[j];
    					if(i==0 && treg.indexOf('time:') > -1){//时间格式化
			    			temp.push(arr[j])
			    			break;
			    		}
    					if(i==1 && treg.indexOf('round:') > -1){//四舍五入
			    			temp.push(arr[j])
			    			break;
			    		}
    					if(i==2 && treg.indexOf('split:') > -1){//财务分割
			    			temp.push(arr[j])
			    			break;
			    		}
    					if(i==3 && treg.indexOf('upper:') > -1){//加前缀
			    			temp.push(arr[j])
			    			break;
			    		}
    					if(i==4 && treg.indexOf('prefix:') > -1){//加前缀
			    			temp.push(arr[j])
			    			break;
			    		}
    					if(i==5 && treg.indexOf('suffix:') > -1){//加后缀
			    			temp.push(arr[j])
			    			break;
			    		}
    				}
    			}
    			if(this.val != null){
    				this.formatValue = this.val.toString() ;
    			}else{
    				this.formatValue = this.val ;
    			}

    			for(let k=0;k<temp.length;k++){
    				let reg = temp[k];


		    		if(reg.indexOf('time:') > -1){//时间格式化
		    			if(this.formatValue){
		    				this.formatValue = dateFormat(this.formatValue,reg.substring(5))
		    			}
		    		}

		    		if(reg.indexOf('round:') > -1){
		    			if(this.formatValue){
		    				this.formatValue = round(this.formatValue,reg.substring(6));
//		    				if(this.formatValue.indexOf(".")>-1){
//		    					let num = new Number(this.formatValue)
//		    					this.formatValue = num.toFixed(reg.substring(6))
//		    				}else{
//
//		    				}
		    			}
		    		}

		    		if(reg.indexOf('split:') > -1){
		    			if(this.formatValue){
		    				console.log(this.formatValue);
		    				var subs = reg.substring(6);

		    				if(subs.indexOf(":")==-1){//如果没冒号 按财务格式分割
		    					if(this.formatValue.toString().indexOf(".")>-1){
			    					let regstr = "(\\d)(?=(\\d{"+ subs +"})+\\.)"
			    					let regex = new RegExp(regstr,'g');
			    					this.formatValue = this.formatValue.toString().replace(regex, '$1,');
			    				}else{
			    					let regstr = "(\\d)(?=(?:\\d{"+ subs +"})+$)"
			    					let regex = eval(`/${regstr}/g`);
			    					this.formatValue = this.formatValue.toString().replace(regex, '$1,');
			    				}
		    				}else{
		    					let tarr = subs.split(':');
		    					let regstr = "(.{"+ tarr[0] +"})"
			    				let regex = new RegExp(regstr,'g');
			    				this.formatValue = this.formatValue.toString().replace(regex, '$1'+tarr[1]);
			    				let tstr = this.formatValue.substring(this.formatValue.length - tarr[1].length)
			    				if(tstr == tarr[1]){
			    					this.formatValue = this.formatValue.substring(0, this.formatValue.length - tarr[1].length)
			    				}
		    				}

		    			}
		    		}

//		    		digitUppercase  upper
					if(reg.indexOf('upper:') > -1){
						if(this.formatValue){
		    				this.formatValue = digitUppercase(this.formatValue)
		    			}
					}

		    		if(reg.indexOf('prefix:') > -1){
		    			if(this.formatValue){
		    				this.formatValue = reg.substring(7) + this.formatValue;
		    			}
		    		}

		    		if(reg.indexOf('suffix:') > -1){
		    			if(this.formatValue){
		    				this.formatValue = this.formatValue + reg.substring(7);
		    			}
		    		}

    			}
	    		//小数格式化   。。。  下面继续做小数点格式化

	    	}else{
	    		this.formatValue = this.val;
	    	}
	    	this.inputval = this.formatValue;
//  		let regex = eval(`/${reg}/g`);
    	},
    	blur(e){

    		this.inputval = this.formatValue;
    		this.$emit('blur',e);
    	},
    	focus(e){

    		if(this.readonly||this.disabled){
    			this.inputval = this.formatValue;
    		}else{
    			this.inputval = this.realvalue;
    		}
    		this.$emit('focus',e);
    	},
    	change(e){
    		this.$emit('change',e);
    	},
    	input(e){//加了会 出现错误

    	},

    }
  }
</script>

<style scoped>
	/*element 输入框样式*/
	>>>.el-input {
		height: 100%;
	}

	>>>.el-input__inner {
		height: 100%;
		border: none;
		border-radius: 0px;
		color: var(--input-text-color);
		font-size: 14px;
		font-family: Microsoft YaHei;
		font-weight: 400;
		text-align: inherit;
	}
	/*element 输入框的 focus hover样式*/
	>>>.el-input__inner:focus,
	>>>.el-textarea__inner:focus {
		background: var(--input-focus-back-color);
	}
	>>>.el-input__inner:hover,
	>>>.el-textarea__inner:hover {
		background: var(--input-hover-back-color);
	}
</style>
